<?php
/**
 * Created by PhpStorm.
 * User: kamlyli
 * Date: 2017/7/22
 * Time: 15:11
 */

namespace app\lib\exception;

use Exception;
use think\exception\Handle;


class ExceptionHandler extends Handle
{
    private $code;
    private $msg;
    private $error_code;

    public function render(Exception $e)
    {
        if ($e instanceof BaseException) {
            // 如果是自定义异常
            $this->code = $e->code;
            $this->msg = $e->msg;
            $this->error_code = $e->error_code;
        } else {
            // 内部错误
            if (config('app_debug')) {
                // return default error page
                return parent::render($e);
            } else {
                $this->code = 500;
                $this->msg = 'unknow_v1_error';
                $this->error_code = 999;
            }
        }

        //  TODO  写错误日志

        // 返回数据
       return json_error($this->msg, $this->error_code, $this->code);
    }
}